﻿' TouchFlo Detacher
' Copyright (C) 2009, Avian Waves Software
' SEE LICENSE.TXT for software licensing information regarding original TouchFlo Detacher source code and binaries
' SEE OpenNetCF_LICENSE.TXT for licensing information regarding OpenNetCF binaries

Imports System.IO
Imports System.Drawing

Public Class SelectFile
  Const icoOpen As Integer = 0
  Const icoClose As Integer = 1
  Const icoFile As Integer = 2

  Private _filter As String = ""
  Private _suppliedFilter As String = ""

  Public Sub New(Optional ByVal filter As String = "")
    MyBase.New()
    Cursor.Current = Cursors.WaitCursor

    Me.InitializeComponent()

    Me._filter = filter
    Me._suppliedFilter = filter

    If Me._filter.Length <= 0 Then
      Me.MenuItemMenu.MenuItems.Remove(Me.MenuItemCancel)
      Me.mainMenu1.MenuItems.Remove(Me.MenuItemMenu)
      Me.mainMenu1.MenuItems.Add(Me.MenuItemCancel)
      Me._filter = "*"
    Else
      Me.MenuItemFilter.Text = "Show " & Me._suppliedFilter
    End If
  End Sub

  Private _FileName As String = ""
  Public ReadOnly Property FileName() As String
    Get
      Return Me._FileName
    End Get
  End Property

  Private Sub SelectFile_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim imgcol As New List(Of Image)

    ' Set the icons to the correct scaling
    Using g As Graphics = Me.CreateGraphics
      Dim scaleFactor As Single = g.DpiX / 96.0

      If scaleFactor <> 1.0 Then
        Me.ImageList1.ImageSize = New Size(Me.ImageList1.ImageSize.Width * scaleFactor, Me.ImageList1.ImageSize.Height * scaleFactor)
        'For i As Integer = 0 To Me.ImageList1.Images.Count - 1
        '  Dim nbmp As New Bitmap(Me.ImageList1.Images(i).Width * scaleFactor, Me.ImageList1.Images(i).Height * scaleFactor)
        '  Dim sr As New Rectangle(0, 0, ImageList1.Images(i).Width, ImageList1.Images(i).Height)
        '  Dim dr As New Rectangle(0, 0, nbmp.Width, nbmp.Height)
        '  Using bg As Graphics = Graphics.FromImage(nbmp)
        '    bg.DrawImage(Me.ImageList1.Images(i), dr, sr, System.Drawing.GraphicsUnit.Pixel)
        '  End Using
        '  imgcol.Add(nbmp)
        'Next

        '' Clear out the image list
        'Me.ImageList1.Images.Clear()

        '' Add all the newly scaled images into the image list
        'For Each im As Image In imgcol
        '  Me.ImageList1.Images.Add(im)
        'Next
      End If
    End Using

    Me.ClearTreeView()
    Cursor.Current = Cursors.Default
  End Sub

  Private Sub ClearTreeView()
    TreeView1.Nodes.Clear()
    Dim node As New TreeNode
    Try
      ' ---create the root node---
      node.ImageIndex = icoClose
      node.SelectedImageIndex = icoOpen
      node.Text = "\"
      TreeView1.Nodes.Add(node)
      ' ---Add the dummy node---
      node.Nodes.Add("")
      node.Expand()
    Catch err As Exception
      MsgBox(err.ToString)
    End Try
  End Sub

  Private Sub MenuItemSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemSelect.Click
    If TreeView1.SelectedNode IsNot Nothing AndAlso TreeView1.SelectedNode.ImageIndex = icoFile Then
      Me._FileName = Me.stripExtraSlash(Me.TreeView1.SelectedNode.FullPath)
      Me.DialogResult = Windows.Forms.DialogResult.OK
      Me.Close()
    Else
      MessageBox.Show("No file is selected.", "Cannot select", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
    End If
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub

  Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
    Cursor.Current = Cursors.WaitCursor
    ' ---if leaf node then exit---
    If e.Node.ImageIndex = icoFile Then Return
    ' ---remove the dummy node and display 
    ' the subdirectories and files---
    Try
      e.Node.Nodes.Clear()  ' clears all the nodes and...
      displayChildNodes(e.Node) ' create the nodes again
    Catch err As Exception
      MsgBox(err.ToString)
    End Try
    ' ---change the icon for this node to open---
    If e.Node.GetNodeCount(False) > 0 Then
      e.Node.ImageIndex = icoClose
      e.Node.SelectedImageIndex = icoOpen
    End If
    Cursor.Current = Cursors.Default
  End Sub

  Private Sub displayChildNodes(ByVal parentNode As System.Windows.Forms.TreeNode)
    Dim FS As New DirectoryInfo(stripExtraSlash(parentNode.FullPath))
    Dim dirInfo As DirectoryInfo
    Dim fileInfo As FileInfo
    Try
      ' ---displays all dirs---
      For Each dirInfo In FS.GetDirectories()
        ' ---create a new node to be added---
        Dim node As New TreeNode
        node.Text = dirInfo.Name ' name of file or dir
        node.ImageIndex = icoClose
        node.SelectedImageIndex = icoOpen
        parentNode.Nodes.Add(node)
        ' ---add the dummy node---
        node.Nodes.Add("")
      Next
    Catch err As Exception
      MsgBox(err.ToString)
    End Try
    Try
      ' --display all files---
      For Each fileInfo In FS.GetFiles(Me._filter)
        ' ---create a new node to be added---
        Dim node As New TreeNode
        node.Text = fileInfo.Name
        node.ImageIndex = icoFile
        node.SelectedImageIndex = icoFile
        parentNode.Nodes.Add(node)
      Next
    Catch err As Exception
      MsgBox(err.ToString)
    End Try
  End Sub

  Public Function stripExtraSlash(ByVal str As String) As String
    ' ---strip away the extra "\" for 
    ' subdirectories. e.g. \\My documents
    Dim path As String
    If str.Length > 1 And (Mid(str, 1, 1) = "\") Then
      path = Mid(str, 2, str.Length - 1)
    Else
      path = str
    End If
    Return path
  End Function

  Private Sub MenuItemAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemAll.Click
    Me._filter = "*"
    Me.ClearTreeView()
  End Sub

  Private Sub MenuItemFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemFilter.Click
    Me._filter = Me._suppliedFilter
    Me.ClearTreeView()
  End Sub
End Class